package com.example.home.attorney.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.attorney.bean.AttorneyCommentBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class AttorneyCommentAdapter extends RecyclerView.Adapter<AttorneyCommentAdapter.MyHoldy> {
    private Context context;
    private List<AttorneyCommentBean.RowsBean> list;
    private View view;
    private ImageView likeImageView;
    private boolean isLiked = false; // 初始状态为未点赞
    private static final String PREFS_NAME = "MyPrefsFile";

    public AttorneyCommentAdapter(Context context, List<AttorneyCommentBean.RowsBean> list) {
        this.context = context;
        this.list = list;

        // 从SharedPreferences中获取上次保存的点赞状态
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        isLiked = prefs.getBoolean("isLiked", false);
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.e("xhf","1321");
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attcom,null);
        return new AttorneyCommentAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        likeImageView = holder.likeImageView;
        Glide.with(context).load(ip+list.get(position).getFromUserAvatar()).into(holder.imgUrl);
        holder.name.setText(list.get(position).getFromUserName());
        holder.evaluateContent.setText(list.get(position).getEvaluateContent());
        holder.createTime.setText(list.get(position).getCreateTime());
        holder.likeCount.setText(String.valueOf(list.get(position).getLikeCount()));
        // 设置点击事件
        holder.likeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击图像时切换点赞状态
                isLiked = !isLiked;

                // 根据点赞状态更新图像资源
                updateLikeImageViewState();
                // 刷新单个项，而不是整个列表
                notifyItemChanged(position);
                // 在这里可以执行其他与点赞相关的操作，例如发送点赞请求到服务器等
            }
        });


    }


    @Override
    public int getItemCount() {
        if (list.size() >=15){
            return 15;
        }else {
                return list == null ? 0 : list.size();
            }
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView imgUrl;
        private TextView name;
        private TextView evaluateContent;
        private TextView createTime;
        private TextView likeCount;
        private ImageView likeImageView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            evaluateContent = itemView.findViewById(R.id.evaluateContent);
            createTime = itemView.findViewById(R.id.createTime);
            likeCount = itemView.findViewById(R.id.likeCount);
            likeImageView = itemView.findViewById(R.id.likeImageView);

        }
    }

    // 更新图像资源，以反映当前的点赞状态
    private void updateLikeImageViewState() {
        if (isLiked) {
            likeImageView.setImageResource(R.drawable.aixin1);
        } else {
            likeImageView.setImageResource(R.drawable.aixin2);
        }
        // 保存点赞状态到SharedPreferences
        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit();
        editor.putBoolean("isLiked", isLiked);
        editor.apply();
    }

}
